import 'package:flutter/material.dart';
import 'package:live_bg_mobile/common/const/common_const.dart';

import '../common/const/app_style_const.dart';
import '../common/type/data_list_container_type.dart';
import '../common/util/common_util.dart';
import 'card_container.dart';
import 'empty.dart';
import 'refresh_list_indicator.dart';

class DataListContainer extends StatefulWidget {
  Function(BuildContext context, dynamic item) listBodyBuilder;
  Function(DataListContainerQueryParam param) getData;
  bool usePagination;

  DataListContainer(
      {Key? key,
      required this.listBodyBuilder,
      required this.getData,
      this.usePagination = true})
      : super(key: key);

  @override
  DataListContainerState createState() => DataListContainerState();
}

class DataListContainerState extends State<DataListContainer> {
  bool isLastPage = false;
  List<dynamic> dataList = [];
  int currentPage = 1;

  @override
  void initState() {
    super.initState();
    getData();
  }

  @override
  void dispose() {
    super.dispose();
  }

  getData() async {
    DataListContainerQueryParam param = DataListContainerQueryParam();

    if (widget.usePagination) {
      param.pageSize = pageSize;
      param.currentPage = currentPage;
    }

    var res = await widget.getData(param);

    if (null != res.data && res.data["isSuccess"]) {
      if (null != res.data["data"] && res.data["data"].length > 0) {
        dataList.addAll(res.data["data"]);
        isLastPage = widget.usePagination ? false : true;
      } else {
        isLastPage = true;
      }
      setState(() {});
    } else {
      CommonUtil.showToast(res.data["msg"]);
    }
  }

  Future<void> refresh() async {
    CommonUtil.textInputHide();
    CommonUtil.debounce(() async {
      if (widget.usePagination) {
        currentPage = 1;
      }

      isLastPage = false;
      dataList = [];
      await getData();
    }, null)();
  }

  Future<void> loadMore() async {
    if (widget.usePagination) {
      currentPage++;
      await getData();
    }
  }

  Widget buildList() {
    return Padding(
      padding: const EdgeInsets.all(10.0),
      child: Column(
        children: dataList
            .map(
              (item) => Container(
                child: widget.listBodyBuilder(context, item),
              ),
            )
            .toList(),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return dataList.isNotEmpty
        ? RefreshListIndicator(
            onRefresh: refresh,
            onLoadMore: loadMore,
            isLastPage: isLastPage,
            child: buildList(),
          )
        : Padding(
            padding: const EdgeInsets.only(top: 15.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const Empty(
                  label: '暂无数据，',
                ),
                InkWell(
                  onTap: refresh,
                  child: const Text("点击重试",style: dataListContainerReloadTxtStyle,),
                )
              ],
            ),
          );
  }
}
